package com.flyco.modules.system.controller;

import com.flyco.common.api.vo.Result;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.constant.CacheConstant;
import com.flyco.common.constant.CommonConstant;
import com.flyco.common.system.util.JwtUtil;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.service.IStmStoreInfoService;
import com.flyco.modules.system.entity.SysDepart;
import com.flyco.modules.system.model.SysDepartStoreModel;
import com.flyco.modules.system.model.SysDepartTreeModel;
import com.flyco.modules.system.service.ISysDepartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Api(tags = "部门门店")
@RestController
@RequestMapping("/f/sys/sysDepartStore")
@Slf4j
public class SysDepartStoreController {

    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private IStmStoreInfoService stmStoreInfoService;


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "部门门店-通过id查询")
    @ApiOperation(value="部门门店-通过id查询", notes="部门门店-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        StmStoreInfo storeInfo = stmStoreInfoService.getById(id);
        SysDepart depart = sysDepartService.getById(id);
        SysDepartStoreModel model = new SysDepartStoreModel();
        BeanUtils.copyProperties(storeInfo, model);
        BeanUtils.copyProperties(depart, model);
        return Result.ok(model);
    }

    /**
     * 添加新数据 添加用户新建的部门对象数据,并保存到数据库
     *
     * @param sysDepartStoreModel
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @CacheEvict(value = {CacheConstant.SYS_DEPARTS_CACHE, CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries = true)
    public Result<?> add(@RequestBody SysDepartStoreModel sysDepartStoreModel, HttpServletRequest request) {
        stmStoreInfoService.saveDepartStoreData(sysDepartStoreModel);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑数据 编辑部门的部分数据,并保存到数据库
     *
     * @param sysDepart
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @CacheEvict(value = {CacheConstant.SYS_DEPARTS_CACHE, CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries = true)
    public Result<?> edit(@RequestBody SysDepartStoreModel sysDepart, HttpServletRequest request) {
        Boolean ok = stmStoreInfoService.editDepartStoreDataById(sysDepart);
        if (ok) {
            return Result.ok("修改成功!");
        }
        return Result.error("修改失败!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @CacheEvict(value = {CacheConstant.SYS_DEPARTS_CACHE, CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries = true)
    public Result<SysDepart> delete(@RequestParam(name = "id", required = true) String id) {

        Result<SysDepart> result = new Result<SysDepart>();
        SysDepart sysDepart = sysDepartService.getById(id);
        if (sysDepart == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = sysDepartService.delete(id);
            if (ok) {
                //清除部门树内存
                //FindsDepartsChildrenUtil.clearSysDepartTreeList();
                // FindsDepartsChildrenUtil.clearDepartIdModel();
                result.success("删除成功!");
            }
        }
        return result;
    }


    /**
     * 批量删除 根据前端请求的多个ID,对数据库执行删除相关部门数据的操作
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    @CacheEvict(value = {CacheConstant.SYS_DEPARTS_CACHE, CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries = true)
    public Result<SysDepart> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {

        Result<SysDepart> result = new Result<SysDepart>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.sysDepartService.deleteBatchWithChildren(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }
}
